<template>
  <div class="app-container">
    <el-card shadow="hover">
      <el-descriptions class="margin-top" title="云服务" :column="3" :size="size" border>
        <template slot="extra">
          <el-button type="primary" size="small" @click="isConnection">检查云服务状态</el-button>
        </template>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user" />
            云服务状态：
          </template>
          {{ cloudInfo.status }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone" />
            当前域名：
          </template>
          {{ cloudInfo.domain }}
        </el-descriptions-item>
      </el-descriptions>

    </el-card>
    <el-tabs v-model="product_id" class="margin-top" @tab-click="handleClick">
      <el-tab-pane v-for="(item,index) in products" :key="index" :label="item.title" :name="item.identifie">
        <!-- license		授权码
        product_id		授权标识
        client		客户端
        auth_type		授权类型：1、addons2shop3backend
        activate_license		激活码
        version
        is_latest		是否最新版本
        latest_version		最新版
        license_status		授权校验状态1授权成功2授权失败
        create_time	datetime			False	False
        download_time 最后更新时间
        latest_summary		最新版更新说明
        summary		当前版本更新说明 -->
        <el-card v-loading="upLoading" shadow="hover">
          <div v-if="isProduct">
            <el-descriptions class="margin-top" :title="item.title" :column="3" :size="size" border>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-office-building" />
                  授权产品标识：
                </template>
                {{ productInfo.product_id }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-tickets" />
                  授权码：
                </template>
                {{ productInfo.license }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-tickets" />
                  激活时间：
                </template>
                {{ productInfo.create_time }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user" />
                  授权域名：
                </template>
                {{ productInfo.client }}
              </el-descriptions-item>
              <el-descriptions-item :span="2">
                <template slot="label">
                  <i class="el-icon-tickets" />
                  授权校验状态：
                </template>
                {{ productInfo.license_status_label }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-location-outline" />
                  当前版本：
                </template>
                {{ productInfo.version }}
              </el-descriptions-item>
              <el-descriptions-item :span="2">
                <template slot="label">
                  <i class="el-icon-tickets" />
                  是否最新版本：
                </template>
                {{ productInfo.is_latest_label }}
              </el-descriptions-item>
              <el-descriptions-item :span="6">
                <template slot="label">
                  <i class="el-icon-tickets" />
                  当前版本更新说明：
                </template>
                <div v-html="productInfo.summary" />
              </el-descriptions-item>

              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-location-outline" />
                  最新版：
                </template>
                {{ productInfo.latest_version }}
              </el-descriptions-item>

              <el-descriptions-item :span="2">
                <template slot="label">
                  <i class="el-icon-tickets" />
                  最后更新时间：
                </template>
                {{ productInfo.download_time }}

              </el-descriptions-item>
              <el-descriptions-item :span="6">
                <template slot="label">
                  <i class="el-icon-tickets" />
                  最新版更新说明：
                </template>
                <div v-html="productInfo.latest_summary" />
              </el-descriptions-item>

            </el-descriptions>
            <el-row
              class="margin-top"
              :gutter="20"
              justify="center"
              style="display: flex;justify-content: center;"
            >
              <el-button type="primary" size="small" @click="getLastVersion">查看最新版</el-button>
              <el-button type="primary" size="small" @click="checkUp('main')">更新最新版</el-button>
              <el-button type="primary" size="small" @click="checkUp('sql')">更新数据库</el-button>
              <el-button type="primary" size="small" @click="activateLicenseFunc()">重新激活授权</el-button>
            </el-row>
          </div>
          <div v-else>
            <el-form ref="form" :model="form" label-width="80px">
              <el-form-item label="授权码">
                <el-input v-model="form.license" placeholder="联系管理员，获取授权码" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="activateLicenseFunc">激活授权</el-button>
              </el-form-item>
            </el-form>
          </div>

        </el-card>
        </el-descriptions></el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { fetchAuth } from '@projectName/api/addons.js'
import { activateLicense, checkConnection, checkLocalLicenseExist, checkUpdate, download, getLatestVersion, verifyLicense, versionInfo } from './api.js'
export default {
  data() {
    return {
      upLoading: false,
      products: [],
      listen: '',
      product_id: 'auth_ddiot',
      productInfo: { },
      size: 'mini',
      upType: 'main',
      isProduct: false,
      downloadContent: '',
      type: 1,
      form: {
        listen: '',
        client: ''
      },
      cloudInfo: {
        status: '连接成功'
      },
      LastVersion: {
        latest_version: '',
        message: '',
        product_name: '店滴云授权版',
        release_date: '2024-09-27',
        status: true,
        summary: '测试新版本发布'
      }
    }
  },
  created() {
    this.getAuthList()
    this.getVersionInfo()
    // this.activateLicenseFunc()
    // this.verifyLicenseFunc()
    this.isConnection()
    // this.LicenseExist()
    // this.getLastVersion()
    // this.checkUp()
    this.cloudInfo.domain = window.location.origin.replace('https://', '').replace('http://', '')
  },
  methods: {
    getAuthList() {
      fetchAuth().then((res) => {
        console.log(res)
        const arr = [
          {
            title: '管理后台',
            type: 1,
            identifie: 'auth_ddiot'
          }
        ]
        const obj = res.data.map(item => {
          return {
            title: item.title,
            type: 2,
            identifie: item.identifie
          }
        })
        this.products = arr.concat(obj)
      })
    },
    handleClick(tab, event) {
      this.product_id = tab.name
      this.type = this.products.filter(item => item.identifie === tab.name)[0].type
      console.log('handleClick', tab, event)

      this.getVersionInfo()
    },
    getVersionInfo() {
      versionInfo({
        'type': this.type,
        'product_id': this.product_id
      }).then((res) => {
        console.log(res)
        this.productInfo = res.data.product
        this.isProduct = true
        this.$message({
          message: '获取成功',
          type: 'success'
        })
      }).catch((err) => {
        console.log(err)
        this.isProduct = false
        this.$message({
          message: err.message,
          type: 'error'
        })
      })
    },
    activateLicenseFunc() {
      console.log('activateLicense', window.location.origin)
      /**
       * 获取当前域名
       */
      this.form.client = window.location.origin.replace('https://', '').replace('http://', '')

      activateLicense({
        'type': this.type,
        'product_id': this.product_id,
        'license': this.form.license ? this.form.license : this.productInfo.license,
        'client': this.form.client,
        'product_name': this.LastVersion.product_name
      }).then((res) => {
        console.log(res)
        this.getVersionInfo()
      })
    },
    isConnection() {
      console.log('initPage')
      checkConnection({
        'type': this.type,
        'addons': 'diandi_tea',
        'product_id': this.product_id
      }).then((res) => {
        console.log(res)
        this.$set(this.cloudInfo, 'status', res.data.message)
        this.$message({
          type: 'success',
          message: res.message
        })
      })
    },
    LicenseExist() {
      console.log('LicenseExist')
      checkLocalLicenseExist({
        'type': this.type,
        'addons': 'diandi_tea',
        'product_id': this.product_id
      }).then((res) => {
        console.log(res)
      })
    },
    getLastVersion() {
      console.log('getLastVersion')
      getLatestVersion({
        'type': this.type,
        'product_id': this.product_id
      }).then((res) => {
        console.log(res)
        this.getVersionInfo()
        this.LastVersion = res.data
      }).catch((err) => {
        console.log(err)
      })
    },
    checkUp(upType) {
      this.upType = upType
      checkUpdate({
        'type': this.type,
        'up_type': this.upType, // main /sql
        'product_id': this.product_id,
        current_version: this.productInfo.after_version ? this.productInfo.after_version : this.productInfo.version
      }).then((res) => {
        console.log(res)
        if (res.data.status) {
          this.getVersionInfo()
          console.log('有新版本')
          this.$confirm('有新版本,是否更新最新版本？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.upLoading = true
            this.upFile(res.data)
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消更新'
            })
          })
        } else {
          console.log('没有新版本')
          this.$message({
            message: res.data.message,
            type: 'success'
          })
        }
      })
    },
    upFile(upData) {
      console.log('upFile')
      download({
        'type': this.type,
        'update_id': upData.update_id,
        'product_id': this.product_id,
        'up_type': this.upType, // main /sql
        'version': upData.version,
        'license': this.productInfo.license,
        'client': this.productInfo.client
      }).then((res) => {
        console.log(res)
        this.downloadContent = res.data
        this.upLoading = false
        this.getVersionInfo()

        this.$message({
          message: '更新成功',
          type: 'success',
          duration: 2000,
          onClose: () => {
            /**
             * 强制刷新页面
             */
            window.location.reload()
          }
        })
      }).catch((err) => {
        console.log(err)
        this.upLoading = false
        this.$message({
          message: err.message,
          type: 'success'
        })
      })
    },
    verifyLicenseFunc() {
      console.log('verifyLicense')
      verifyLicense({
        'type': this.type,
        'addons': 'diandi_tea',
        'product_id': this.product_id,
        'time_based_check': '1',
        'license': '938823JR',
        'client': '王春生',
        'product_name': this.LastVersion.product_name
      }).then((res) => {
        console.log(res)
        this.$message({
          message: '验证授权成功',
          type: 'success'
        })
      })
    }
  }
}
</script>

<style>

</style>
